package com.ruoyi.merchant.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class RabbitMQConfig {
    //声明交换机
    //正常交换机
    @Bean("ex01")
    public Exchange getExchange() {
        return ExchangeBuilder.topicExchange("ex11").durable(true).build();
    }

    //死信交换机
    @Bean("fex2")
    public Exchange getDlxExchange() {
        return ExchangeBuilder.topicExchange("fex2").durable(true).build();
    }

    //声明队列
    //正常队列
    @Bean("q01")
    public Queue getQueue() {
        return QueueBuilder
                .durable("q11")
                .ttl(5*1000)
                .deadLetterExchange("fex2")
                .deadLetterRoutingKey("rescue.dead")
                .build();
    }

    //死信队列
    @Bean("q02")
    public Queue getDlxQueue() {
        return QueueBuilder.durable("q22").build();
    }

    //绑定队列和交换机
    //正常交换机和正常队列绑定
    @Bean
    public Binding getBinding(Queue q01, Exchange ex01) {
        return BindingBuilder.bind(q01).to(ex01).with("rescue.#").noargs();
    }
    //死信交换机和死信队列绑定
    @Bean
    public Binding getDlxBinding(Queue q02, Exchange fex2) {
        return BindingBuilder.bind(q02).to(fex2).with("rescue.#").noargs();
    }
}
